<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
         <XtxBreadItem :to="`/category/${goods.categories[1].id}`">
          {{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">
          {{ goods.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem to="/">{{goods.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品信息左边 -->
        <div class="goods-info-left">
            <GoodsImage :images="goods.mainPictures"></GoodsImage>
             <GoodsSales />
        </div>
          <div class="spec">
         <GoodsName :goods="goods"/>
         <!-- sku商品选中组件 -->
          <GoodsSku :key="goods.id" :goods="goods"  @change='changeSku'/>
  <XtxNumbox label='数量' v-model="count"  :max='goods.inventory' :min='1'/>
  <!-- 购物车按钮组件 -->
   <XtxButton style="margin-top:20px" type="primary" @click="addTocaetBtn"
            >加入购物车</XtxButton
          >
        </div>

        <!-- 商品信息右边 -->
        <div class="goods-info-right"></div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId='goods.id' />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs/>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
<GoodsWarn />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId='goods.id' :type='1'/>
           <GoodsHot :goodsId='goods.id' :type='2'/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, watch, provide } from 'vue'
import { useRoute } from 'vue-router'
import GoodsRelevant from './components/goods-relevant.vue'
import { findGoods } from '@/api/goods.js/product'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import XtxNumbox from '@/components/library/xtx-numbox.vue'
import GoodsSku from './components/goods-sku.vue'
import XtxButton from '@/components/library/xtx-button.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import Message from '@/components/library/Message'
import { useStore } from 'vuex'

export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsSales,
    GoodsName,
    XtxNumbox,
    GoodsSku,
    XtxButton,
    GoodsTabs,
    GoodsHot,
    GoodsWarn

  },
  setup () {
    const store = useStore()
    const route = useRoute()
    const goods = ref(null)
    watch(() => route.params.id,
      async id => {
        if (route.path !== `/product/${id}`) return
        const { result } = await findGoods(id)
        // console.log(result)
        goods.value = result
      },
      { immediate: true }
    )
    const count = ref(1)

    const changeSku = val => {
      console.log('父组件接收到选中的规格数据', val)
      if (val.skuId) {
        goods.value.inventory = val.inventory
        goods.value.price = val.price
        goods.value.oldPrice = val.oldPrice
        goods.value.skuId = val.skuId
        goods.value.attrsText = val.attrsText
      } else {
        goods.value.skuId = ''
      }
    }

    provide('goods', goods)
    // console.log(goods)
    // 购物车按钮
    // const goodsitem = ref(null)
    const addTocaetBtn = async () => {
      // console.log(11)
      if (!goods.value.skuId) return Message({ type: 'warn', message: '请选择完整的规格' })
      const goodsItem = {
        id: goods.value.spuCode, // SPUID
        name: goods.value.name, // 商品名称
        price: goods.value.oldPrice, // 加入时价格
        nowPrice: goods.value.price, // 当前的价格
        skuId: goods.value.skuId, // SKUID
        attrsText: goods.value.attrsText, // 属性文字，例如“颜色:瓷白色 尺寸：8寸”
        stock: goods.value.inventory || goods.value.stock, // 库存
        count: count.value, // 🔔数量
        picture: goods.value.mainPictures[0], // 🔔图片
        selected: true, // 是否选中
        isEffective: true // 是否有效
      }
      console.log(goods)
      // console.log(goodsItem)
      // 🔔初级写法：mutations 用 commit 调用
      // store.commit('cart/insertCart', goodsItem)
      // 🔔高级写法：actions 用 dispatch 调用，dispatch 内部再调用 commit
      // 调用仓库里面的方法，然后传一个goodsItem的数据进去，在里面做逻辑，方法
      const res = await store.dispatch('cart/insertCart', goodsItem)
      Message({ type: 'success', message: res })
    }
    return { goods, count, changeSku, addTocaetBtn }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
  .goods-tabs {
  min-height: 600px;
 background: #fff;
}
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
